﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Components;
using Components.Enum;
using Model;
using MsDAL;
using System.ComponentModel;
using ServiceHelper.Doc;
using ServiceHelper.Util;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Sto6a00Manager : Sto6a00Base
    {
        private static Sto6a00Manager _entity;

        public static Sto6a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Sto6a00Manager();
                return _entity;
            }
        }

        #region 个人代码

        public string SaveData(Hashtable tb, string action, string usercode)
        {
            var model = new Sto6a00();
            CommonUtil.GetInstance.CopyProperties(tb, model);

            if (model.Storageqty == model.OldStorageQty)
                throw new StnException("库存未发生变化，无需调整");

            //生成库存调整单
            var adjust = new AdjustManager();
            adjust.Parse(model);

            return adjust.Save();
        }

        /// <summary>
        /// 批量保存
        /// </summary>
        /// <returns></returns>
        public string MeditSaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            var adjust = new AdjustManager();

            var doc = adjust.CreateDefaultModel();
            doc.Remark1 = Validation.ToString(tb["remark1"]);
            doc.Warehouseid = Validation.ToString(tb["storageid"]);

            adjust.Parse(doc);

            foreach (DataRow dr in dtl.Rows)
            {
                var dtlModel = new Sto6a00();
                CommonUtil.GetInstance.CopyProperties(dtlModel, dr);

                var row = adjust.ParseRow(dtlModel); //如果调整数量为零，不会加入到保存行中
                if (null != row)
                    row.Dtlremark = dtlModel.Remark1;
            }

            return adjust.Save();
        }

        #endregion

        /// <summary>
        /// 记账，增加库存
        /// </summary>
        /// <param name="model"></param>
        /// <param name="positive">是否是正记账 </param>
        /// <param name="retryTimes"> </param>
        public void AddStorageQty(Sto8a01 model, bool positive = true, int retryTimes = 0)
        {
            if (retryTimes > 100)
                throw new Exception("数据库正忙，无法完成记录库存操作");

            var old = GetModel(model.Dtlwarehouseid, model.Merchid);
            bool isAdd = false;
            if (old != null)
            {
                old.OldStorageQty = model.OldStorageQty ?? old.Storageqty;
            }

            if (old == null)
            {
                old = new Sto6a00();

                old.Storageid = model.Dtlwarehouseid;
                old.Pid = model.Merchid;
                old.Listid = Sys0a60Manager.Entity.CreateSn("sto6a00_listid", ServerContext.UserCode);
                old.Storageqty = 0;
                isAdd = true;
            }

            var storageQty = AnalysisSymbol(model.Dtldoctype) * model.Dtlstockqty;

            if (positive == false)
                storageQty = 0 - storageQty;

            old.Storageqty += storageQty;

            try
            {
                if (isAdd)
                    CommonManager.Entity.SaveData(sto6a00Dal.Add(old));
                else
                    CommonManager.Entity.SaveData(sto6a00Dal.Update(old));
            }
            catch (Exception ex)
            {
                AddStorageQty(model, positive, ++retryTimes);
            }
        }

        private int AnalysisSymbol(string documentType)
        {
            if (documentType == DocumentType.PurchaseReturn)
                return -1;
            else if (documentType == DocumentType.SendAdjust)
                return -1;
            else if (documentType == DocumentType.SendDoc)
                return -1;

            return 1;
        }

        public Sto6a00 GetModel(string warehouseId, string productId)
        {
            string where = string.Format("storageid=@storageid and pid=@pid");
            Hashtable param = QB.Entity.AddParam("@storageid", warehouseId)
                                       .AddParam("@pid", productId);

            return GetModel(where, param);
        }
    }
}



